//
//  HomeTableViewController.swift
//  WeiBo
//
//  Created by 刘星 on 16/6/6.
//  Copyright © 2016年 LiuXing. All rights reserved.
//

import UIKit
import AFNetworking

let HomeReuseIdentifier = "HomeReuseIdentifier"
class HomeTableViewController: BaseTableViewController {

    // 保存闭包数据
    var statuses: [Status]? {
        didSet {
            tableView.reloadData()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // 设置未登录界面的信息
        if !userLogin {
            visitorView?.setupVisitorInfo(true, imageName: "visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜")
            return
        }
        // 设置Item
        setupNav()
        // 注册一个cell
        tableView.registerClass(StatusTableViewCell.self, forCellReuseIdentifier: HomeReuseIdentifier)
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        // 加载微博数据
        loadData()
    }
    private func setupNav() {
        navigationItem.leftBarButtonItem = UIBarButtonItem.crateBarButtonItem("navigationbar_friendattention", target: self, action: #selector(HomeTableViewController.letBtnClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem.crateBarButtonItem("navigationbar_pop", target: self, action: #selector(HomeTableViewController.rightBtnClick))
    }
    func letBtnClick() {
        print(#function)
    }
    func rightBtnClick() {
        print(#function)
    }
    private func loadData() {
        Status.loadStatuses { (models, error) in
            self.statuses = models
        }
    }
}
extension HomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeReuseIdentifier, forIndexPath: indexPath) as! StatusTableViewCell
        let status = statuses![indexPath.row]
        cell.status = status
        return cell
    }
}